// pages/interest/interest.js
const app = getApp();
const request = require("../../utils/request.js").request;
const url = require("../../utils/request.js").commonUrl;
const util = require("../../utils/util.js");
const collectRequest = require("../../utils/util.js").collectRequest;
const collectUtil = require("../../utils/collect-util.js");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        url: url,
        weixin: "",
        list: [],
        nodata: false,
        hideLoading: true
    },
    getlist: function () {
        let that = this;
        let param = {
            "weixin": that.data.weixin,
            "searchInterest": true
        }

        request("/m/activity/interest/list.json", param).then(function (res) {
            console.log(app.globalData.header);
            console.log(res);
            if (res.data.success) {
                let list = res.data.result;
                if (list.length != 0) {
                    that.setData({
                        nodata: false,
                        hideLoading: true,
                        list: list
                    });
                    for (let i = 0; i < that.data.list.length; i++) {
                        let collectObsBean = new collectUtil.CollectObsBean();
                        // console.log(collectObsBean.Tag);
                        collectObsBean.tag = collectUtil.Tag.interest;
                        collectObsBean.activityId = that.data.list[i].activityId;

                        collectObsBean.callBack = function CallBack(result) {
                            that.data.list[i]["interest"] = result[0];
                            that.data.list[i]["numberOfInterest"] = result[1];
                            //TODO 改成局部更新
                            that.setData(that.data);
                            console.log(that.data.re);

                        };
                        collectUtil.addActivity(collectObsBean);
                    }

                } else {
                    that.setData({
                        nodata: true
                    })
                }

            } else {
                wx.showToast({
                    title: '列表加载失败',
                    icon: '',
                    image: '',
                    duration: 2000,
                    mask: true,
                    success: function (res) {
                    },
                    fail: function (res) {
                    },
                    complete: function (res) {
                    },
                })
            }

        })
    },
    like: function (event) {
        let that = this;
        util.isLogin(function () {
            let result = [];
            let index = event.currentTarget.dataset.starid;
            let value = !that.data.list[index].interest;

            const params = {
                "activityId": that.data.list[index].activityId, //活动点ID
                "weixin": app.globalData.openid,  //用户ID
                "isInterest": value//是否收藏，true为收藏，false为取消收藏
            };
            util.collectRequest(params, function (isInterest, count) {
                let isInterestKey = 'list[' + index + '].interest';
                let isInterestValue = isInterest;
                let countKey = 'list[' + index + '].numberOfInterest';
                let countVal = count;

                let interestParam = {}, countParam = {};
                interestParam[isInterestKey] = isInterestValue;
                countParam[countKey] = countVal;
                console.log(isInterest, count);

                that.setData(interestParam);
                that.setData(countParam);

                // result.push(isInterestValue);
                // result.push(countVal);
                // collectUtil.collectActivity(that.data.list[index].activityId, result);
            });

            app.globalData.flag = that.data.list[index].activityId;
        })

    },
    //进入详情页
    detailGo: function (event) {
        var that = this;
        let index = event.currentTarget.dataset.itemid;
        let activityId = this.data.list[index].activityId;
        let activityAreaId = this.data.list[index].activityAreaId;
        var detailInfo = {
            activityId: activityId,
            activityAreaId: activityAreaId
        }
        that.setData({
            index: index
        });

        wx.navigateTo({
            url: '../details/details?detailInfo=' + JSON.stringify(detailInfo),
            success: function (res) {
            },
            fail: function (res) {
            },
            complete: function (res) {
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            weixin: app.globalData.openid,
            hideLoading: false
        });

        this.getlist();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})